package sk.ygor.raynor.client.service;

import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.rpc.AsyncCallback;
import sk.ygor.raynor.client.json.JSONSerializer;

public interface JSONService {

    public static interface Configuration {

        @SuppressWarnings({"UnusedDeclaration"})
        // used in generated code
        public void setInvoker(Invoker invoker);

        @SuppressWarnings({"UnusedDeclaration"})
        // used in generated code
        public void setTransformer(Transformer transformer);

        @SuppressWarnings({"UnusedDeclaration"})
        // used in generated code
        public void setSerializerTransformer(JSONSerializer.Transformer serializerTransformer);

    }

    public static interface Transformer {

        @SuppressWarnings({"UnusedDeclaration"})
        // used in generated code
        public String transformExternalServiceName(String servicePackageName,
                                                   String serviceClassName);

    }


    public static class DefaultTransformer implements Transformer {

        @Override
        public String transformExternalServiceName(String servicePackageName, String serviceClassName) {
            return servicePackageName + "." + serviceClassName;
        }
    }

    public static class SimpleTransformer implements Transformer {

        @Override
        public String transformExternalServiceName(String servicePackageName, String serviceClassName) {
            return serviceClassName;
        }
    }

    public static interface Invoker {

        @SuppressWarnings({"UnusedDeclaration"})
        // used in generated code
        public void invoke(String serviceName,
                           String methodName,
                           AsyncCallback<JSONValue> callback,
                           Parameter... parameters);

        public static class Parameter {

            public final String name;
            public final JSONValue value;

            public Parameter(String name, JSONValue value) {
                this.name = name;
                this.value = value;
            }
        }
    }
}
